using NetProtocol.POD;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public static class GameDataCenter
    {
        /// <summary>
        /// 游戏服的玩家数据
        /// </summary>
        public static PlayerData PlayerData { get; private set; }
        /// <summary>
        /// 当前所在区域
        /// </summary>
        public static GameplayAreaType CurrGameArea => GameplayAreaManager.Instance.curAreaType;
        /// <summary>
        /// 当前所在关卡
        /// </summary>
        public static int curLevelCid => CurrGameArea == GameplayAreaType.Level ? -1 : LevelPlayModule.Instance.levelPlay.cfgLevel.Id;
        /// <summary>
        /// 当前登录的服务器信息
        /// </summary>
        public static LoginResponseServerInfo CurrGameServerInfo;
        /// <summary>
        /// 客户端显示的时间（玩家可以自己修改，下线不保存）
        /// </summary>
        public static DateTime ClientDisplayTime => _clientDisplayTime.AddSeconds(UnityEngine.Time.realtimeSinceStartup - _changeDisplayTimeUnitySec);
        private static DateTime _clientDisplayTime;
        /// <summary>
        /// 玩家修改显示时间时的unity真实时间，由于玩家设置的显示时间也需要跟随真实时间速度流逝，
        /// 所以需要用它和当前时间的差值计算后续ClientDisplayTime
        /// </summary>
        private static float _changeDisplayTimeUnitySec;

        public static void Init(PlayerPOD playerPOD)
        {
            //注意 playerPOD仅用于初始化Module, 不要持有PlayerPOD
            PlayerData = new PlayerData();
            SetClientTime(GameServerModule.Instance.ServerTime);
            PlayerModule.Instance.InitModule(playerPOD);
            ItemModule.Instance.InitModule(playerPOD);
            EquipmentModule.Instance.InitModule(playerPOD);
            HeroModule.Instance.InitModule(playerPOD);
            FormationModule.Instance.InitModule(playerPOD);
            QuestModule.Instance.InitModule(playerPOD);
            MailModule.Instance.InitModule(playerPOD);
            MallModule.Instance.InitModule(playerPOD);
            PetModule.Instance.InitModule(playerPOD);
            SettingModule.Instance.InitModule(playerPOD);
            MainCityModule.Instance.InitModule(playerPOD);
            FunctionModule.Instance.InitModule();
            FriendModule.Instance.InitModule(playerPOD);
        }

        public static void Clear()
        {
            PlayerModule.Instance.ClearModule();
            ItemModule.Instance.ClearModule();
            EquipmentModule.Instance.ClearModule();
            HeroModule.Instance.ClearModule();
            FormationModule.Instance.ClearModule();
            QuestModule.Instance.ClearModule();
            MailModule.Instance.ClearModule();
            MallModule.Instance.ClearModule();
            PetModule.Instance.ClearModule();
            SettingModule.Instance.ClearModule();
            MainCityModule.Instance.ClearModule();
            FunctionModule.Instance.ClearModule();
            FriendModule.Instance.ClearModule();
            PlayerData = null;
        }

        /// <summary>
        /// 设置客户端用于显示的时间（玩家可以自行设置，未设置前跟随服务器时间，设置的时间登出即失效）
        /// </summary>
        /// <param name="targetTime"></param>
        public static void SetClientTime(DateTime targetTime)
        {
            _clientDisplayTime = targetTime;
            _changeDisplayTimeUnitySec = UnityEngine.Time.realtimeSinceStartup;
        }
    }
}
